<?php

/**
 * Node Model
 *
 */
class Application_Model_Node
{
    const DB_TABLE='nodes';
    const RESOURCE_NAME='node';
    
    protected $_type;
    protected $_data;
    
    public function __construct($node=null)
    {
        if (!empty($node) && (is_array($node) || (is_object($node) && ($node instanceof Zend_Db_Table_Row))))
        {
            $this->_init($node);
        } else 
        {
            $this->_init(array(
                'id' => 0,
                'title' => '',
                'short_text' => '',
                'full_text' => '',
                'published_date' => '',
                'published' => 0,
                'front' => 0,
                'uid' => 0,
                'type' => Application_Model_NodeTypes::getDefaultType(),
                'category' => Application_Model_NodeCategories::getDefaultCategory(),
                'url_alias' => ''
            ));
        } 
    }
    
    /**
     * Magic function
     *
     * @param string $var
     */
    public function __get($var)
    {
        return $this->_getData($var);
    }
    
    /**
     * Magic function
     *
     * @param string $var
     * @param mixed $val
     */
    public function __set($var,$val)
    {
        $this->_setData($var,$val);
    }
    
    /**
     * Magic function
     *
     * @param string $var
     */
    public function __unset($var)
    {
        if (array_key_exists($var,$this->_data))
        {
            unset($this->_data[$var]);
        }
    }
    
    /**
     * Magic function
     *
     * @param string $var
     * @return bool
     */
    public function __isset($var)
    {
        return array_key_exists($var,$this->_data);
    }
    
    /**
     * Magic function
     *
     * @return string
     */
    public function __toString()
    {
        return (string) $this->full_text;
    }
    
    /**
     * Initializes node
     *
     * @param mixed $user
     */
    protected function _init($node)
    {
        foreach ($node as $var=>$val)
        {
            $this->_setData($var,$val);
        }
    }
    
    /**
     * Gets variable value
     *
     * @param string $var
     * @return mixed
     */
    protected function _getData($var)
    {
        $method='_getValue'.ucwords($var);
        
        if (method_exists($this,$method))
        {
            return $this->{$method}();
        }
        
        return null;
    }
    
    /**
     * Sets variable value
     *
     * @param string $var
     * @param mixed $val
     */
    protected function _setData($var,$val)
    {
        $method='_setValue'.ucwords($var);
        
        if (method_exists($this,$method))
        {
            $this->{$method}($val);
        }
    }

    /**
     * Returns user ID
     *
     * @return int
     */
    protected function _getValueId()
    {
        return (int) $this->_data['id'];
    }
    
    /**
     * Returns node title
     *
     * @return string
     */
    protected function _getValueTitle()
    {
        return (string) $this->_data['title'];
    }
    
    /**
     * Returns node short text
     *
     * @return string
     */
    protected function _getValueShort_text()
    {
        return (string) $this->_data['short_text'];
    }
    
    /**
     * Returns node full text
     *
     * @return string
     */
    protected function _getValueFull_text()
    {
        return (string) $this->_data['full_text'];
    }
    
    /**
     * Returns node publish date
     *
     * @return string
     */
    protected function _getValuePublish_date()
    {
        return (string) $this->_data['publish_date'];
    }
    
    /**
     * Returns node published flag
     *
     * @return int
     */
    protected function _getValuePublished()
    {
        return (int) $this->_data['published'];
    }
    
    /**
     * Returns node front flag
     *
     * @return int
     */
    protected function _getValueFront()
    {
        return (int) $this->_data['front'];
    }
    
    /**
     * Returns node published user ID
     *
     * @return int
     */
    protected function _getValueUid()
    {
        return (int) $this->_data['uid'];
    }
    
    /**
     * Returns node type
     *
     * @return string
     */
    protected function _getValueType()
    {
        return (string) $this->_data['type'];
    }
    
    /**
     * Returns node category name
     *
     * @return string
     */
    protected function _getValueCategory()
    {
        return (string) $this->_data['category'];
    }
    
    /**
     * Returns node URL alias
     *
     * @return string
     */
    protected function _getValueUrl_alias()
    {
        return (string) $this->_data['url_alias'];
    }
    
    /**
     * Returns node language
     *
     * @return string
     */
    protected function _getValueLanguage()
    {
        return (string) $this->_data['language'];
    }
    
    /**
     * Returns node meta data
     *
     * @return array
     */
    protected function _getValueMeta()
    {
        
        $meta=array();
        
        if (!empty($this->_data['meta']))
        {
            try {
                $data=Zend_Registry::get('serializer')->unserialize($this->_data['meta']);
                if (is_array($data))
                {
                    $meta=$data;
                }
            } catch (Zend_Serializer_Exception $e) {}
        }
         
        return $meta;
    }
    
     /* Sets node ID
     *
     * @param int $val
     */
    protected function _setValueId($val)
    {
        $this->_data['id']=(int) $val;
    }
    
     /* Sets node title
     *
     * @param string $val
     */
    protected function _setValueTitle($val)
    {
        $this->_data['title']=(string) $val;
    }
    
    /* Sets node short text
     *
     * @param string $val
     */
    protected function _setValueShort_text($val)
    {
        $this->_data['short_text']=(string) $val;
    }
    
    /* Sets node full text
     *
     * @param string $val
     */
    protected function _setValueFull_text($val)
    {
        $this->_data['full_text']=(string) $val;
    }
    
    /* Sets node published date
     *
     * @param string $val
     */
    protected function _setValuePublish_date($val)
    {
        $this->_data['publish_date']=(string) $val;
    }
    
    /* Sets node published flag
     *
     * @param int $val
     */
    protected function _setValuePublished($val)
    {
        $this->_data['published']=(int) $val;
    }
    
    /* Sets node front flag
     *
     * @param int $val
     */
    protected function _setValueFront($val)
    {
        $this->_data['front']=(int) $val;
    }
    
    /* Sets node published user ID
     *
     * @param int $val
     */
    protected function _setValueUid($val)
    {
        $this->_data['uid']=(int) $val;
    }
    
    /* Sets node type
     *
     * @param string $val
     */
    protected function _setValueType($val)
    {
        $this->_data['type']=(string) Application_Model_NodeTypes::isValidType($val) ? $val : Application_Model_NodeTypes::getDefaultType();
    }
    
    /**
     * Sets node category
     *
     * @param string $val
     */
    protected function _setValueCategory($val)
    {
        $this->_data['category']=(string) Application_Model_NodeCategories::isValidCategory($val) ? $val : Application_Model_NodeCategories::getDefaultCategory();
    }
    
    /**
     * Sets node URL alias
     *
     * @param string $val
     */
    protected function _setValueUrl_alias($val)
    {
        $this->_data['url_alias']=$val;
    }
    
    /**
     * Sets node language
     *
     * @param string $val
     */
    protected function _setValueLanguage($val)
    {
        $this->_data['language']=$val;
    }
    
    /**
     * Sets node meta data
     *
     * @param array $val
     */
    protected function _setValueMeta($val)
    {
        $data=$val;
        $meta=array();
        
        if (is_array($val))
        {
           try {
                $data=Zend_Registry::get('serializer')->serialize($val);
           } catch (Zend_Serializer_Exception $e) {}
        }

        $this->_data['meta']=$data;
    }
    
    /**
     * Checks if node is exists
     *
     * @return bool
     */
    public function isExists()
    {
        return $this->_data['id'] > 0 ? true : false;
    }
    
    /**
     * Checks if node is published
     *
     * @return bool
     */
    public function isPublished()
    {
        return $this->_data['published'] ? true : false;
    }
    
     /**
     * Returns an array with node data
     *
     * @return array
     */
    public function toArray()
    {
        return $this->_data;
    }
    
    /**
     * Returns node keywords
     *
     * @return string
     */
    public function getKeywords()
    {
        return !empty($this->meta['keywords']) ? $this->meta['keywords'] : '';
    }
    
    /**
     * Returns node meta description
     *
     * @return string
     */
    public function getDescription()
    {
        return !empty($this->meta['description']) ? $this->meta['description'] : '';
    }
    
    /**
     * Returns node meta thumbnail
     *
     * @return string
     */
    public function getThumbnail()
    {
        return !empty($this->meta['thumbnail_filename']) && !empty($this->meta['thumbnail_ext']) ? array($this->meta['thumbnail_filename'],$this->meta['thumbnail_ext']) : array('','');
    }
    
    /**
     * Returns node type human name
     *
     * @return string
     */
    public function getType()
    {
        return Application_Model_NodeTypes::getType($this->type);
    }
    
    /**
     * Returns node category human name
     *
     * @return string
     */
    public function getCategory()
    {
        return Application_Model_NodeCategories::getCategory($this->category);
    }
    
    /**
     * Sets node keywords
     *
     * @param string
     */
    public function setKeywords($keywords)
    {
        $meta=$this->meta;
        $meta['keywords']=$keywords;
        $this->meta=$meta;
    }
    
    /**
     * Sets node meta description
     *
     * @param string
     */
    public function setDescription($description)
    {
        $meta=$this->meta;
        $meta['description']=$description;
        $this->meta=$meta;
    }
    
    /**
     * Sets node meta thumbnail
     *
     * @param string
     */
    public function setThumbnail($thumbnail,$ext)
    {
        $meta=$this->meta;
        $meta['thumbnail_filename']=$thumbnail;
        $meta['thumbnail_ext']=$ext;
        $this->meta=$meta;
    }
}

